package com.bsit.obu.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;


public class DES {
	public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";
	

//    public static String Encode(String key,String data) {
//    	if(data == null)
//    		return null;
//    	try{
//    		IvParameterSpec zeroIv = new IvParameterSpec(key.getBytes());  
//    		SecretKeySpec keySpec = new SecretKeySpec("12345678".getBytes(), "DES");  
//    		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");  
//    		cipher.init(Cipher.ENCRYPT_MODE, keySpec, zeroIv); 
//
//    		
//	    	/*DESKeySpec dks = new DESKeySpec(key.getBytes());	    	
//	    	SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
//	        //key鐨勯暱搴︿笉鑳藉灏忎簬8浣嶅瓧鑺�
//	        Key secretKey = keyFactory.generateSecret(dks);
//	        Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
//	        IvParameterSpec iv = new IvParameterSpec(key.getBytes());
//	        AlgorithmParameterSpec paramSpec = iv;
//	        cipher.init(Cipher.ENCRYPT_MODE, secretKey); */          
//	        byte[] bytes = cipher.doFinal(data.getBytes());            
//	        return Byte2Hex(bytes);
//    	}catch(Exception e){
//    		e.printStackTrace();
//    		return data;
//    	}
//    }

   
//    public static String Decode(String key,String data) {
//    	if(data == null)
//    		return null;
//        try {
//	    	DESKeySpec dks = new DESKeySpec(key.getBytes());
//	    	SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
//            //key鐨勯暱搴︿笉鑳藉灏忎簬8浣嶅瓧鑺�
//            Key secretKey = keyFactory.generateSecret(dks);
//            Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
//            IvParameterSpec iv = new IvParameterSpec(key.getBytes());
//            AlgorithmParameterSpec paramSpec = iv;
//            cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
//            return new String(cipher.doFinal(Hex2Byte(data.getBytes())));
//        } catch (Exception e){
//    		e.printStackTrace();
//    		return data;
//        }
//    }

	/**
	 * 浜岃鍒惰浆瀛楃涓�
	 * @param b
	 * @return
	 */
    public static String Byte2Hex(byte[] b) {
		StringBuilder hs = new StringBuilder();
		String stmp;
		for (int n = 0; b!=null && n < b.length; n++) {
			stmp = Integer.toHexString(b[n] & 0XFF);
			if (stmp.length() == 1)
				hs.append('0');
			hs.append(stmp);
		}
		return hs.toString().toUpperCase();
	}
    
    public static byte[] Hex2Byte(byte[] b) {
        if((b.length%2)!=0)
            throw new IllegalArgumentException();
		byte[] b2 = new byte[b.length/2];
		for (int n = 0; n < b.length; n+=2) {
		    String item = new String(b,n,2);
		    b2[n/2] = (byte)Integer.parseInt(item,16);
		}
        return b2;
    }
    
	/**
	 * 16杩涘埗瀛楃涓茶浆鎹㈡垚int鏁扮粍
	 *
	 * @param str
	 * @return
	 */
	public static byte[] hexStr(String str) {

		byte[] bt = new byte[str.length() / 2];
		for (int i = 0; i < str.length() / 2; i++) {
			bt[i] = (byte) Integer.parseInt(str.substring(i * 2, 2 * (i + 1)),
					16);
		}
		return bt;
	}
	/**
	 * bytes杞崲鎴愬崄鍏繘鍒跺瓧绗︿覆
	 */
	public static String byte2HexStr(byte[] a) {
		String hs = "";
		String stmp;
		for (int n = 0; n < a.length; n++) {
			stmp = (Integer.toHexString(a[n] & 0XFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
		}
		return hs.toUpperCase();
	}
	/**
     * DES绠楁硶锛屽姞瀵�
     *
     * @param data 寰呭姞瀵嗗瓧绗︿覆
     * @param key  鍔犲瘑绉侀挜锛岄暱搴︿笉鑳藉灏忎簬8浣�
     * @return 鍔犲瘑鍚庣殑瀛楄妭鏁扮粍锛屼竴鑸粨鍚圔ase64缂栫爜浣跨敤
     * @throws InvalidAlgorithmParameterException 
     * @throws Exception 
     */
	public static String encryptDES(String encryptString, String encryptKey) throws Exception {
		SecretKeySpec key = new SecretKeySpec(Hex2Byte(encryptKey.getBytes()), "DES"); 
        Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding"); 
        cipher.init(Cipher.ENCRYPT_MODE, key); 
        byte[] encryptedData = cipher.doFinal(Hex2Byte(encryptString.getBytes()));
	 
		return Byte2Hex(encryptedData);//Base64.encode(encryptedData);
	}
	 /**
     * DES绠楁硶锛岃В瀵�
     *
     * @param data 寰呰В瀵嗗瓧绗︿覆
     * @param key  瑙ｅ瘑绉侀挜锛岄暱搴︿笉鑳藉灏忎簬8浣�
     * @return 瑙ｅ瘑鍚庣殑瀛楄妭鏁扮粍
     * @throws Exception 寮傚父
     */
	public static String decryptDES(String decryptString, String decryptKey) throws Exception {
		SecretKeySpec key = new SecretKeySpec(Hex2Byte(decryptKey.getBytes()), "DES"); 
        Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding"); 
        cipher.init(Cipher.DECRYPT_MODE, key); 
        byte decryptedData[] = cipher.doFinal(Hex2Byte(decryptString.getBytes())); 
	 
		return  Byte2Hex(decryptedData);
	}

}